package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.BannerQuery;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelsService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelsService travelsService;
    //获取所有数据并共享给页面
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BannerQuery qo){
        Page<Banner> page = bannerService.query(qo);
        model.addAttribute("page",page);
        return "/banner/list";
    }
    //保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        if(StringUtils.hasLength(banner.getId())){
            bannerService.update(banner);
        }else {
            banner.setId(null);
            bannerService.save(banner);
        }
        return JsonResult.success();
    }
    //删除操作
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        bannerService.delete(id);
        return  JsonResult.success();
    }
    //获取所有的攻略和游记,并将其分为两个键值对分别存储
    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType(){
        //先新建一个map集合
        Map<String,Object> map = new HashMap<>();
        //再将所有的攻略存入
        map.put("sts",strategyService.list());
        //再将所有的游记存入
        map.put("ts",travelsService.list());
        //最后返回 map 集合过去即可
        return  JsonResult.success(map);
    }
    //修改当前banner状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id,int state){
        bannerService.changeState(id,state);
        return  JsonResult.success();
    }
}
